package com.fang.testController;

import com.fang.Entity.TaskCondition;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

@Controller
public class TestController {
    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        System.out.println(4);

        try {
//            int i = 1/0;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(777);
        }


        return "maw";
    }

    @RequestMapping("/test2")
    public String test2() {
        System.out.println(4);

        return "mav";
    }

    @RequestMapping("/test3")
    public String test3() {
        TaskCondition taskCondition = new TaskCondition(false);
        TaskCondition taskCondition1 = new TaskCondition(true);
        List list = new ArrayList();
        list.add(taskCondition);
        list.add(taskCondition1);

//        model.addAttribute("list", list);
        return "Test";
    }

    @RequestMapping("/download")
    public void test4(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Content-Type", "video/mp4");
        response.setHeader("Content-Disposition", "attachment;filename=hyj.mp4");

        URLConnection urlConnection = new URL("http://startpage.soufunimg.com/searchAD/80932ef4-5e85-49d2-888a-d335a703be57.mp4").openConnection();
        InputStream inputStream = urlConnection.getInputStream();
        ServletOutputStream outputStream = response.getOutputStream();

        byte[] b = new byte[1024];
        int length = 0;
        while (-1 != (length = inputStream.read(b))) {
            outputStream.write(b);
        }
        outputStream.close();
        inputStream.close();
    }

    /**
     * thymeleaf 测试
     */
    @RequestMapping("/thy")
    public String test5() {
        System.out.println(666);
        return "Test";
    }
}
